package com.qingcloud.adminbackend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingcloud.adminbackend.anno.SysApi;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.role.*;
import com.qingcloud.adminbackend.service.ISysOrgService;
import com.qingcloud.adminbackend.service.ISysRoleService;
import com.qingcloud.adminbackend.utils.CookieUtil;
import com.qingcloud.adminbackend.utils.DateUtil;
import com.qingcloud.base.annotation.LoginUser;
import com.qingcloud.base.remote.pojo.LoginUserDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wangjinnan
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysOrgService sysOrgService;

    @ApiOperation(value = "获取用户身份", notes = "通过角色类型获取用户身份", response = SysRole.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleType", value = "用户类型 1网格化 2室内空气", required = true, dataType = "string", paramType = "query",
                    defaultValue = "1")
    })
    @GetMapping("/getRoleIdentity")
    public AjaxResult getRoleIdentity(String roleType, HttpServletRequest request) {
        if (StringUtils.isEmpty(roleType)) {
            return new AjaxResult(WamingConstant.FAILURE, "身份类型不能为空", null);
        }
        String userId = CookieUtil.getUserId(request);
        return new AjaxResult(WamingConstant.SUCCESS, null, sysRoleService.getRoleIdentity(roleType, userId));
    }

    @SysApi
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping(value = "/addSysRole")
    public AjaxResult addSysRole(@RequestBody SysRoleDto role, HttpServletRequest request) {
        String loginUserId = CookieUtil.getUserId(request);
        return sysRoleService.addSysRole(role, loginUserId);
    }

    @SysApi
    @ApiOperation(value = "修改角色", notes = "修改角色")
    @PostMapping(value = "/updateSysRole")
    public AjaxResult updateSysRole(@RequestBody SysRoleDto role, HttpServletRequest request) {
        String loginUserId = CookieUtil.getUserId(request);
        role.setModifyBy(loginUserId);
        role.setModifyDt(DateUtil.getCurrentDate());
        return sysRoleService.updateSysRole(role);
    }

    @SysApi
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping(value = "/deleteRoles")
    public AjaxResult deleteRoles(@RequestBody RoleDelRequest roleDelRequest, HttpServletRequest request) {
        String loginUserId = CookieUtil.getUserId(request);
        return sysRoleService.deleteRoles(roleDelRequest.getRoleIds(), loginUserId);
    }

    @SysApi
    @ApiOperation(value = "id查询角色", notes = "查询角色")
    @ApiImplicitParam(name = "roleId", value = "角色ID", dataTypeClass = Integer.class, paramType = "path")
    @GetMapping(value = "/id/{roleId}")
    public AjaxResult findRolesByIds(@PathVariable("roleId") Integer roleId) {
        return sysRoleService.findRolesByIds(new Integer[]{roleId});
    }

    @SysApi
    @ApiOperation(value = "身份角色表格分页查询", notes = "身份角色表格分页查询")
    @PostMapping(value = "/page")
    public AjaxResult<IPage<SysRoleTableDTO>> page(@RequestBody RolePageRequest rolePageRequest,@LoginUser LoginUserDTO loginUserDTO) {
        if(loginUserDTO==null || loginUserDTO.getUserId()==null)
            return AjaxResult.fail("无法获取用户信息");
        String userId =  loginUserDTO.getUserId();
        rolePageRequest.setUserId(userId);
        return new AjaxResult(WamingConstant.SUCCESS, "", sysRoleService.page(rolePageRequest));
    }

    @SysApi
    @ApiOperation(value = "查询所有角色", notes = "查询角色")
    @PostMapping(value = "/findRoles")
    public AjaxResult findRoles() {
        return sysRoleService.findRoles();
    }

    @SysApi
    @ApiOperation(value = "获取功能类型列表", notes = "获取功能类型列表")
    @GetMapping(value = "/getFuncTypes")
    public AjaxResult getFuncTypes() {
        return sysRoleService.getFuncTypes();
    }
}

